﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dycox.Reporting
{
    public class DataSheetConfig
    {
        public DataSheetConfig()
        {
            _columns = new DataSheetColumnCollection(this);
        }

        private DataSheetColumnCollection _columns;

        public DataSheetColumnCollection Columns
        {
            get { return _columns; }
        }

        private Style _captionStyle;

        public Style CaptionStyle
        {
            get { return _captionStyle; }
            set { _captionStyle = value; }
        }

        private string _caption;

        public string Caption
        {
            get { return _caption; }
            set { _caption = value; }
        }

        public DataSheet CreateSheet(IEnumerable dataSource)
        {
            if (dataSource == null)
                throw new ArgumentNullException("dataSource");


            DataSheet sheet = new DataSheet(this);
            
            foreach (object dataItem in dataSource)
            {
                DataSheetRow row = new DataSheetRow();
                sheet.Rows.Add(row);

                foreach (DataSheetColumn col in _columns)
                {
                    DataSheetCell cell = new DataSheetCell(col);

                    try
                    {
                        cell.Value = col.GetValue(dataItem);
                    }
                    catch
                    {
                    }

                    row.Cells.Add(cell);
                }
            }

            return sheet;
        }
    }
}
